<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';
?>

<div id="subcontent">
<form method="post" action="" class="hidden-print"> 
  <center><h3>Show Game Comment</h3></center>
	<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        
	<select name="season" class="form-control" id="select1">

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

	echo "<option id=\"selected-season\" value=\"$setseason\" selected=\"selected\">$setseason is selected</option>";
} else {
	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
}


$sql = "SELECT DISTINCT season 
		FROM games
		ORDER BY season";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};


?>

	</select>
	</div>
	</div>


<br> <br>
<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        

<select name="selectedgame" class="form-control" id="select2">

<?php 

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {

	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	echo "<option id=\"selected-game\" value=\"$selectedgame\" selected=\"selected\">$setseason game $selectedgame selected</option>";         

} else {
	echo "<option id=\"selected-game\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a game</option>";
}


$sql = "SELECT gamenumber, date, opponent, location
FROM games
WHERE season = '$setseason'
ORDER BY date DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 
    $gamenumber = $row['gamenumber'];
    $date = $row['date'];
    $opponent = $row['opponent'];
    $location = $row['location'];

  echo "<option value='$gamenumber'>Game $gamenumber $date ($opponent $location)  </option>";
}



?>
  </select>

</div>
</div>

</form>
</div>

<div id="comment">
	<Br><br>
<?php
	if (isset($_POST['season']) && isset($_POST['selectedgame'])) {
	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	$sql4 = "SELECT comment, gamenumber, date, opponent, location FROM games 
	WHERE season = '$setseason' 
	AND gamenumber = '$selectedgame'";

	$result4=$db->query($sql4);
	$row4=$result4->fetch_assoc();

	$currentcomment = $row4['comment'];

	echo "<center><h4> Game Comment of $season game $gamenumber $date ($opponent $location)</h4><br></center>";

	echo nl2br($currentcomment);

}

?>




</div>


<script>

$("#select1").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_viewgamecomment.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_viewgamecomment.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});



</script>